﻿/*
// Exepack.NET compressor
// http://www.codeplex.com/exepack
//
// Main form
// Written by Y [14-02-09]
// Copyright (c) 2008-2010 Alexey Yakovlev
*/

using System;
using System.Reflection;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Exepack
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
			Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
			UserInterfaceHelper.BuildMenus(this, ToolBar, MainMenu);
			TreeImages.LoadImages(AssemblyTypes.Unspecified, AssemblyTypes.Executable, AssemblyTypes.Library, AssemblyTypes.Unused);
			OptionsView = new CompressionOptionsView(TreeView);
		}

		/// <summary>
		/// Compression options for the executable file
		/// </summary>
		CompressionOptionsView OptionsView { get; set; }

		[ToolButton("New", Image = "Exepack.Images.New.png"), MenuItem("File", "New")]
		void New(object sender, EventArgs args)
		{
			if (OptionsView.CompressionOptions != null)
			{
				if (MessageBox.Show("Are you sure you want to start from scratch?", 
					"Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
					return;
			}

			OptionsView.Clear();
			PropertyGrid.SelectedObject = null;
		}

		[ToolButton("Add file...", Image = "Exepack.Images.Add.png"), MenuItem("File", "Add file...")]
		void AddFiles(object sender, EventArgs args)
		{
			if (OpenFileDialog.ShowDialog() == DialogResult.OK)
			{
				AddFiles(OpenFileDialog.FileNames);
			}
		}

		[ToolButton("Compress", Image = "Exepack.Images.Go.png"), MenuItem("File", "Compress")]
		void Compress(object sender, EventArgs args)
		{
			if (OptionsView.CompressionOptions == null)
			{
				MessageBox.Show("Add files to compress");
				return;
			}

			using (var c = new ExeCompressor())
			using (var f = new ProgressForm(this, c))
			{
				f.Show();
				c.CompressExecutable(OptionsView.CompressionOptions);
			}
		}

		[ToolButton("Exit", Image = "Exepack.Images.Exit.png"), MenuItem("File", "Exit", Separated = true)]
		void Exit(object sender, EventArgs args)
		{
			Close();
		}

		[MenuItem("Help", "About")]
		void About(object sender, EventArgs args)
		{
			using (var f = new AboutForm(this))
			{
				f.ShowDialog();
			}
		}

		private void AddFiles(string[] files)
		{
			foreach (string s in files)
			{
				OptionsView.Add(s);
			}
		}

		private void TreeView_DragEnter(object sender, DragEventArgs args)
		{
			if (args.Data.GetDataPresent(DataFormats.FileDrop))
			{
				args.Effect = DragDropEffects.Copy;
			}
		}

		private void TreeView_DragDrop(object sender, DragEventArgs args)
		{
			string[] files = (string[])args.Data.GetData(DataFormats.FileDrop);
			AddFiles(files);
		}

		private void TreeView_AfterSelect(object sender, TreeViewEventArgs args)
		{
			PropertyGrid.SelectedObject = OptionsView[TreeView.SelectedNode];
		}
	}
}
